Skip to content

Python: Add type attribution tests, use moduleName for module-qualified FQNs, and fix RemoveImport shadowing#6798

Merged
knutwannheden merged 8 commits intomainfrom
add-type-attribution-tests
Feb 23, 2026
Merged

Python: Add type attribution tests, use moduleName for module-qualified FQNs, and fix RemoveImport shadowing#6798
knutwannheden merged 8 commits intomainfrom
add-type-attribution-tests

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

@knutwannheden knutwannheden commented Feb 23, 2026

Summary

  • Add comprehensive type attribution tests across 13 parser test files (assign, binary, class, collection_literal, def, field_access, for, import, lambda, method_declaration, method_invocation, ternary, type_hint, unary) covering return types, declaring types, parameter types, generic type parameters, and call-site type arguments
  • Add import type attribution tests verifying all import styles (qualified, from-import, aliased, aliased from-import) resolve to the same declaring type
  • classLiteral types now use moduleName from ty-types to build proper FQNs (e.g. mymodule.MyClass instead of bare MyClass)
  • Bare function calls (e.g. join("a", "b") imported via from os.path import join) now get a declaring type from moduleName
  • RemoveImport now uses type attribution to detect shadowed locals — from os.path import join is correctly removed when join is only used as a local variable name in a function scope
  • Fix UUID-based temp filenames by prefixing with _ to ensure valid Python module names (ty-types omits moduleName when filenames start with a digit)
  • Remove @requires_ty_cli and @requires_module_name skip markers since ty-types minimum version is pinned in pyproject.toml
  • Bump ty-types to 0.0.19.dev20260223122104 which provides the moduleName field
  • Add field_type attribution on function/method parameter identifiers and call-site type arguments for generic functions

Test plan

  • 419 tree parser tests pass (including ~40 new type attribution tests)
  • 31 recipe tests pass (including ChangeImport shadowing test)
  • 8 RemoveImport tests pass (3 new shadowing tests)
  • Full suite: 987 passed, 11 skipped

- Add type attribution tests to 13 existing parser test files covering
  method invocations, binary ops, type hints, collection literals, imports,
  field access, class instances, method declarations, async defs, for loops,
  unary ops, ternaries, and lambdas
- Add `method_declaration_type()` to type_mapping.py to build JavaType.Method
  for function declarations using ty-types descriptor data with annotation fallback
- Add type attribution to ParameterizedType nodes in type hint expressions
- Fix pre-existing assign_test.py bug (simple_name access, FQN startswith)
- Add typing.Text test to test_type_attribution.py
- Bump ty-types dependency to >=0.0.19.dev20260223093555
- Add param_type_info() to PythonTypeMapping for function parameter
  Identifier nodes to get JavaType.Variable field_type
- Update __convert_name() and map_arg() to flow field_type through to
  J.Identifier and NamedVariable
- Use call signature returnTypeId for call-site-specific return types
  (e.g. int for identity(42) instead of generic T)
- Populate _declared_formal_type_names on method invocation types from
  function descriptor type parameters
- Bump ty-types to 0.0.19.dev20260223102528 for callSignature
  typeArguments/returnTypeId support
classLiteral types now use moduleName to build proper FQNs
(e.g. mymodule.MyClass instead of bare MyClass), and bare
function calls get a declaring type from their module. Bumps
ty-types to 0.0.19.dev20260223122104.
@knutwannheden knutwannheden force-pushed the add-type-attribution-tests branch from 204705e to d311477 Compare February 23, 2026 12:35
- Extract _ty_types_has_module_name() into shared _markers.py module
  to eliminate duplication and reduce subprocess overhead
- Fix temp file leak by adding os.unlink() in finally block
- Fix test_class_instance_type_attribution to accept module-qualified
  FQNs (endsWith 'Foo' instead of exact match)
- Gate from-import tests with @requires_module_name since bare function
  calls need moduleName for declaring type resolution
- Accept both posixpath and ntpath in os.path test for Windows compat
Since ty-types minimum version is pinned in pyproject.toml, the
@requires_ty_cli and @requires_module_name skip markers are unnecessary.
Remove them and the shared _markers.py module.

Also prefix UUID-based temp filenames with underscore to ensure they are
valid Python module names — ty-types omits moduleName when the filename
starts with a digit, causing non-deterministic test behavior.
RemoveImport._is_referenced now skips identifiers inside function
scopes that have field_type set, as these are local variables
shadowing the imported name rather than actual uses of the import.
This prevents keeping unused imports when a local variable happens
to share the same name.

Also fix test_type_attribution assertions to accept module-qualified
FQNs (e.g. 'test.Greeter' instead of bare 'Greeter') since the
moduleName improvements now produce module-prefixed names.
@knutwannheden knutwannheden changed the title Python: Use moduleName from ty-types for module-qualified FQNs Python: Add type attribution tests, use moduleName for module-qualified FQNs, and fix RemoveImport shadowing Feb 23, 2026
@knutwannheden knutwannheden merged commit 727aed2 into main Feb 23, 2026
1 check failed
@knutwannheden knutwannheden deleted the add-type-attribution-tests branch February 23, 2026 13:57
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant